package com.weilx959.spt.core.component;

import com.jfoenix.controls.JFXSnackbar;
import com.weilx959.spt.core.RootPaneContext;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Region;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.scene.text.TextAlignment;
import javafx.util.Duration;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * Vtoast
 *
 * @author prettyZoo
 * @since 2024-04-30
 */
@Slf4j
public class VToast {

    private static final JFXSnackbar bar = new JFXSnackbar();

    public static void error(String message) {
        notification(message, ToastType.ERROR);
    }

    public static void info(String message) {
        notification(message, ToastType.INFO);
    }

    private static void notification(String message, ToastType type) {
        final Region notification = createNotification(message, type);
        if (bar.getPopupContainer() == null) {
            bar.registerSnackbarContainer(RootPaneContext.get());
        }
        bar.enqueue(new JFXSnackbar.SnackbarEvent(notification, Duration.seconds(3)));
    }

    private static Region createNotification(String message, ToastType type) {
        Label label = new Label();
        label.getStyleClass().add(type.getStyleClass());
        label.setPrefHeight(30D);
        label.setPrefWidth(30D);

        Text text = new Text(message);
        text.setWrappingWidth(550);
        text.setTextAlignment(TextAlignment.CENTER);
        text.setFont(Font.font("Verdana", 12));
        text.setFill(Color.WHITE);

        HBox hbox = new HBox(2, label, text);
        hbox.setPrefHeight(50);
        hbox.setMinHeight(20);
        hbox.setMaxHeight(200);
        hbox.setPadding(new Insets(3, 3, 3, 6));
        hbox.autosize();

        hbox.setPrefWidth(-1);
        hbox.setMinWidth(150);
        hbox.setMaxWidth(600);

        hbox.setAlignment(Pos.CENTER_LEFT);
        return hbox;
    }

    @RequiredArgsConstructor
    public enum ToastType {
        ERROR("core-toast-label-error"),
        INFO("core-toast-label-info");

        @Getter
        private final String styleClass;
    }

}
